<template>
	<view>
        <u-popup :show="loginPopupShow" mode="bottom" :round="10" @close="closeLogin" zIndex="999998">
            <view class="f__login">
				<div v-if="loginPopupType == 'inquiry'">
					<view class="title">授权提示 </view>
					<view class="text">请授权手机号完成询价</view>
				</div>
				<div v-else-if="loginPopupType == 'price'">
					<view class="title">授权提示</view>
					<view class="text">请授权手机号完成报价</view>
				</div>
				<div v-else>
					<view class="title">欢迎登录~</view>
					<view class="text">会员登录后可享受更好的服务体验</view>
				</div>
                <view class="loginButton" >
					<!-- #ifdef MP-WEIXIN -->
                  <button class="button marginT" id="agree-btn" :disabled="checked[0] != 1" v-if="loginPopupType == 'inquiry'" open-type="getPhoneNumber|agreePrivacyAuthorization" @getphonenumber="decryptPhoneNumber" @agreeprivacyauthorization="handleAgreePrivacyAuthorization" :style="{background:PrimaryColor,opacity:checked[0] == 1 ? 1 : 0.5}">手机号快捷登录</button><!-- 此功能需微信认证 -->
				  <button class="button marginT" id="agree-btn" :disabled="checked[0] != 1" v-else-if="loginPopupType == 'price'" open-type="getPhoneNumber|agreePrivacyAuthorization" @getphonenumber="decryptPhoneNumber" @agreeprivacyauthorization="handleAgreePrivacyAuthorization" :style="{background:PrimaryColor,opacity:checked[0] == 1 ? 1 : 0.5}">手机号快捷登录</button><!-- 此功能需微信认证 -->
				  <button class="button marginT" id="agree-btn" :disabled="checked[0] != 1" v-else open-type="getPhoneNumber|agreePrivacyAuthorization" @getphonenumber="decryptPhoneNumber" @agreeprivacyauthorization="handleAgreePrivacyAuthorization" :style="{background:PrimaryColor,opacity:checked[0] == 1 ? 1 : 0.5}">手机号快捷登录</button><!-- 此功能需微信认证 -->
                  <div style="text-align: center;color:#888;padding:24rpx;" @click="closeLogin">取消</div>
				  <!-- #endif -->
                  <!-- #ifdef MP-ALIPAY -->
                  <button class="button" :disabled="checked[0] != 1" open-type="getAuthorize" scope="userInfo" @getAuthorize="onAuthorization" :style="{background:PrimaryColor}">支付宝授权登录</button>
                  <button class="button marginT" :disabled="checked[0] != 1" open-type="getAuthorize" scope="phoneNumber" @getAuthorize="decryptPhoneNumber" @error="onAuthError" :style="{background:PrimaryColor}">支付宝手机号登录</button><!-- 此功能需申请 -->
                  <!-- #endif -->
                    <div class="u-flex" style="font-size:24rpx;padding:40rpx 0">
						<u-checkbox-group v-model="checked">
							<u-checkbox  label=" " :name="1"></u-checkbox>
						</u-checkbox-group>
						<div>我已阅读并同意<span style="color:#409eff;padding:0 10rpx" @click="toAbout('register')">用户协议</span>和<span style="color:#409eff;padding:0 10rpx" @click="handleOpenPrivacyContract">隐私协议</span></div>
					</div>
					
					

					<!-- <button class="button" @click="closeLogin" style="background:#fff;margin-top:24rpx;" :style="{border:'2rpx solid '+PrimaryColor,color:PrimaryColor}">
						取 消
					</button> -->
                </view>
            </view>
        </u-popup>
        
	</view>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
// #ifdef MP
import { getPhoneInfo, getUserInfo } from './f-login.js';
// #endif
import { getCurrentRouter } from '@/config/login.js';
import {authMobile,setScanCodeLog} from '@/config/api.js'
import store from '@/store';

var clear;
export default {
    name: 'f-login',
    computed: {
    	...mapState(['loginPopupShow','loginPopupType']),
    },
	data() {
		return {
           // #ifndef MP-ALIPAY
            PrimaryColor: '#1fba1a', //主题色
           // #endif
           // #ifdef MP-ALIPAY
           PrimaryColor: '#007AFF',
           // #endif
           // #ifdef APP-PLUS || H5
           isLoading:false,
           isPhoneLogin:true,//是否显示验证码登录
           // #endif
           // #ifndef APP-PLUS || H5
           isLoading:true,
           // #endif
         
           code: '',  //uni.login获取的code
		   checked:[0]
		};
	},
    watch:{

    },
	created() {
		this.currentRouter = getCurrentRouter()
	},
	methods: {
        ...mapMutations(['setLoginPopupShow','setUserInfo']),
		handleOpenPrivacyContract(){
			 wx.openPrivacyContract({})
		},
		handleAgreePrivacyAuthorization(){
			console.log("同意了")
		},
        //授权手机号登录
        decryptPhoneNumber(e) {
            console.log(e,'授权手机号')
            var that = this
            if(e.detail.code){
            	let parameter = {};
            	parameter.code = e.detail.code;
            	let storeUserInfo = store.state.userInfo;
            	if (!storeUserInfo.userId){ // nvue页面读取不到vuex里面数据，将取缓存
            	    storeUserInfo = uni.getStorageSync('userInfo')
            	}
            	parameter.wxOpenId = storeUserInfo.wxOpenId
				parameter.wxUnionId = storeUserInfo.wxUnionId
				parameter.promoterCode = uni.getStorageSync('promoterCode');  
				let sourceDataNew = uni.getStorageSync('sourceData');
				if (sourceDataNew && Date.now() <= sourceDataNew.expiry) { //没有过期
				    parameter.source = sourceDataNew.source 			
				}
            	console.log("storeUserInfo",storeUserInfo)
            	authMobile(parameter).then((data)=>{
            		setTimeout(()=>{
						if(data.userInfo.newUserMsg != ''){
							uni.showModal({
								title: '提示',
								confirmText:'关闭',
								showCancel:false,
								content: data.userInfo.newUserMsg,
								success: function (res) {
									// if (res.confirm) {
									// 	console.log('用户点击确定');
									// } else if (res.cancel) {
									// 	console.log('用户点击取消');
									// }
								}
							});
						}else{
							uni.showToast({
							    title: '登录成功',
							    icon: 'none'
							});
						}
            		    
            		    this.closeLogin();
            		},100)
					let storeUserInfo = {}
					storeUserInfo.mobile = data.userInfo && data.userInfo.mobile ? data.userInfo.mobile : '';
					storeUserInfo.userHeader = data.userInfo && data.userInfo.userHeader ? data.userInfo.userHeader : ''
					storeUserInfo.userName = data.userInfo && data.userInfo.userName ? data.userInfo.userName : '';
					storeUserInfo.realName  = data.userInfo && data.userInfo.realName ? data.userInfo.realName : '';
					storeUserInfo.userId = data.userInfo && data.userInfo.userId ? data.userInfo.userId : '';
					// storeUserInfo.wxOpenId = data.userInfo && data.userInfo.wxOpenId ? data.userInfo.wxOpenId : '';
					// storeUserInfo.wxUnionId = data.userInfo && data.userInfo.wxUnionId ? data.userInfo.wxUnionId : '';
					storeUserInfo.promoterCode = data.userInfo && data.userInfo.promoterCode ? data.userInfo.promoterCode : '';
					storeUserInfo.isBuyerAuth = data.userInfo && data.userInfo.isBuyerAuth ? data.userInfo.isBuyerAuth : 0;
					storeUserInfo.isSellerAuth = data.userInfo && data.userInfo.isSellerAuth ? data.userInfo.isSellerAuth : 0;
					storeUserInfo.storeId = data.userInfo && data.userInfo.storeId ? data.userInfo.storeId : '';
					storeUserInfo.storeName = data.userInfo && data.userInfo.storeName ? data.userInfo.storeName : '';
					storeUserInfo.permissions = data.userInfo && data.userInfo.permissions ? data.userInfo.permissions : [];
					storeUserInfo.newUserMsg   = data.userInfo && data.userInfo.newUserMsg  ? data.userInfo.newUserMsg   : '';
					storeUserInfo.noAuthMsg = data.userInfo && data.userInfo.noAuthMsg ? data.userInfo.noAuthMsg : '';
					storeUserInfo.token = data.token
					// console.log("storeUserInfo",storeUserInfo)
            		this.setUserInfo(storeUserInfo)
					
					const sourceData = uni.getStorageSync('sourceData');
					if (sourceData && Date.now() <= sourceData.expiry) { //没有过期
					   setScanCodeLog({pcode:sourceData.pcode,source:sourceData.source})					
					}
					if(sourceData){
						uni.removeStorageSync('sourceData');
					}
					if(uni.getStorageSync('qdClosed')){
						uni.removeStorageSync('qdClosed');
					}
					
					
            	})
            	
            }else{
				let text = '';
				if(this.loginPopupType == 'inquiry'){
					text = '询价失败！请正常给与授权！'
				}else if(this.loginPopupType == 'price'){
					text = '报价失败！请正常给与授权！'
				}else{
					text = '操作失败！请正常给与授权！'
				}
				uni.showToast({
				  title: text,
				  duration: 2000,
				  icon: 'none'
				})
            }
          
        
		},
        // 支付宝拒绝
        onAuthError(e){
            uni.showToast({
            	title: '您已拒绝授权~',
            	icon: 'none'
            });
        },
		closeLogin(){
            this.setLoginPopupShow(false);
		},
		
		toAbout(agreementType){
			uni.navigateTo({
			  url: '/pagesUser/user/aboutInfo?agreementType='+agreementType
			}) 
		}
        
       
        
        
        
        
      
        
	}
};
</script>

<style lang="scss" scoped>
.f__login {
    padding: 48rpx 32rpx;
    border-radius: 18rpx 18rpx 0 0;
    z-index: 99;
    position: relative;
    .loginLoading {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(255, 255, 255, .95);
        z-index: 999;
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        justify-content: center;
        align-items: center;
    }
    .logo {
        width: 90rpx;
        height: 90rpx;
        border-radius: 18rpx;
        overflow: hidden;
        .img {
            width: 90rpx;
            height: 90rpx;
        }
    }
    .title {
        font-size: 40rpx;
        font-weight: bold;
        margin-top: 24rpx;
    }
    .text {
        font-size: 24rpx;
        color: #666;
        margin-top: 16rpx;
    }
    .loginButton {
        margin-top: 56rpx;
        .button {
            color: #fff;
            width: 100%;
            height: 92rpx;
        }
        .marginT{
            margin-top: 24rpx;
        }
    }
    .tips {
        margin-top: 24rpx;
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        .left {
            /* #ifndef APP-NVUE */
            display: flex;
            /* #endif */
            flex-direction: row;
        }
        .goBuy {
            font-size: 24rpx;
            /* margin-left: 16rpx; */
            background: none;
            /* #ifndef APP-NVUE */
            display: flex;
            /* #endif */
            flex-direction: row;
            justify-content: flex-start;
            padding: 0;
            margin: 0;
            color: #1fba1a;
        }
    }
}
.loginPhone{
    .form-row {
        position: relative;
        border-bottom: 1rpx solid #e8e8e8;
        line-height: 1;
        margin-top: 24rpx;
        .input{
            font-size: 34rpx;
            line-height: 102rpx;
            height: 94rpx;
            width: 100%;
            box-sizing: border-box;
            font-size: 30rpx;
            padding: 0;
            font-weight: bold;
        }
        .getvcode {
            font-size: 26rpx;
            height: 80rpx;
            color: #333;
            line-height: 80rpx;
            background: #eee;
            min-width: 188rpx;
            text-align: center;
            border-radius: 8rpx;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            right: 0;
            z-index: 11;
            &.forhidden {
                background: #eee;
                color: #cccccc;
            }
        }
    }
    .submit{
        margin-top: 60rpx;
        color: #fff;
        width: 100%;
        border: none;
    }
}
</style>
